#/bin/bash

###################
# author longli 20190119
##################

oldDir=$(pwd)
rootDir="/home/wwwroot"

# 检测PHP环境是否已安装
[ ! -d "$rootDir" ]&&{
  echo "请先安装【php】运行环境"
  exit 1
}

# 检测是否已下载安装包
[ ! -f "qlm_erp.zip" ]&&{
  echo "未检测到安装包，请与技术人员联系"
  exit 2
}

# 检测软件是否已安装过
if [ -d "$rootDir/qlm_erp" ];then
  echo "软件已安装过是否需要卸载重新安装[Y/N]"
  read flag
  tflag=$(echo "$flag" | tr [A-Z] [a-z])
  if [ "$tflag" = "y" ];then
    rm -rf "$rootDir/qlm_erp"
  else
    exit 3
  fi
fi


echo "请输入您的数据库密码即可完成安装"

read password

# 检测数据库密码是否正确
mysql -uroot -p"$password" -e"show databases;" >/dev/null 2>&1
[ "$?" -ne "0" ]&&{
  echo "数据库密码有误请重新输入"
  exit 4
}

if [ ! -d "qlm_erp" ];then
 unzip -o "qlm_erp.zip"
fi

chown -R "www.www" "qlm_erp"

mv qlm_erp "$rootDir"

cd "$rootDir/qlm_erp/install"

mv -f "nginx.conf" "/usr/local/nginx/conf/"
mv -f "qlm_erp.conf" "/usr/local/nginx/conf/vhost"

mysql -uroot -p"$password" -e"source erp8.sql;" >/dev/null 2>&1

cd "$rootDir/qlm_erp/application/config"

sed -i "s/Ab123456/$password/" "database.php"


service nginx restart > /dev/null 2>&1
service php-fpm restart > /dev/null 2>&1

# 生产环境需要打开注释
#rm -f "qlm_erp.zip" "install.sh"

echo "安装完成...."
echo "请在浏览器上使用你服务器的【ip】地址访问, 如已绑定域名可使用域名访问"






